/*
 * Copyright (C) 2019 ByteDance Inc
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.bytedance.scene.group;

import com.bytedance.scene.SceneComponentFactory;
import com.bytedance.scene.utlity.AttrValue;
import com.bytedance.scene.utlity.Utility;
import ohos.aafwk.content.IntentParams;
import ohos.agp.components.AttrSet;
import ohos.agp.components.Component;
import ohos.agp.utils.TextTool;
import ohos.app.Context;

public final class ScenePlaceHolderView extends Component implements Component.EstimateSizeListener {
    private String mSceneName;
    private String mSceneTag;
    private IntentParams mSceneArguments;
    private SceneComponentFactory mSceneComponentFactory;

    public ScenePlaceHolderView(Context context, AttrSet attrs) {
        this(context, attrs, 0);
    }

    public ScenePlaceHolderView(Context context, AttrSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        setEstimateSizeListener(this);
        init(context, attrs);
    }

    private void init(Context context, AttrSet attrs) {
        mSceneName = AttrValue.get(attrs, "name", "");
        mSceneTag = AttrValue.get(attrs, "tag", "");
    }

    public void setSceneComponentFactory(SceneComponentFactory callback) {
        this.mSceneComponentFactory = callback;
    }

    public SceneComponentFactory getSceneComponentFactory() {
        return this.mSceneComponentFactory;
    }

    public void setSceneTag(String tag) {
        this.mSceneTag = Utility.requireNonEmpty(tag, "ScenePlaceHolderView tag can't be empty");
    }

    public String getSceneTag() {
        if (TextTool.isNullOrEmpty(mSceneTag)) {
            throw new IllegalArgumentException("ScenePlaceHolderView tag is empty, invoke setSceneTag first");
        }
        return this.mSceneTag;
    }

    public void setSceneName(String name) {
        this.mSceneName = Utility.requireNonEmpty(name, "ScenePlaceHolderView name can't be empty");
    }


    public String getSceneName() {
        if (TextTool.isNullOrEmpty(mSceneName)) {
            throw new IllegalArgumentException("ScenePlaceHolderView name is empty, invoke setSceneName first");
        }
        return this.mSceneName;
    }

    public void setArguments(IntentParams bundle) {
        this.mSceneArguments = bundle;
    }


    public IntentParams getArguments() {
        return this.mSceneArguments;
    }

    private static int getDefaultSize2(int size, int measureSpec) {
        int result = 0;
        int specMode = MeasureSpec.getMode(measureSpec);
        int specSize = MeasureSpec.getSize(measureSpec);

        switch (specMode) {
            case EstimateSpec.UNCONSTRAINT:
                result = size;
                break;
            case EstimateSpec.NOT_EXCEED:
                result = Math.min(size, specSize);
                break;
            case EstimateSpec.PRECISE:
                result = specSize;
                break;
        }
        return result;
    }

    @Override
    public boolean onEstimateSize(int i, int i1) {
        setEstimatedSize(getDefaultSize2(getMinWidth(), i), getDefaultSize2(getMinHeight(), i1));
        return false;
    }
}
